//运营经理关联
function myFunction(tep){

    if(tep==1){
        var manager=$("#sale_manager").val();
        var user_data = {
            param:manager,
        }
        var succCallBack = function (data, status, response) {
            console.log(data)
            if(data.errorCode=="0"){
                var jsonObj =data.data;
                if(jsonObj==""){
                    $("#tuan").html("<li>无数据</li> ");
                }else {
                    console.log(jsonObj)
                    var optionstring = "";
                    for (var  i= 0; i < jsonObj.length; i++) {
                        optionstring += "<li onclick='relation("+ jsonObj[i].user_id+",\""+jsonObj[i].user_name+"\")' data-align=\"" + jsonObj[i].user_name + "\" > " + jsonObj[i].user_name + "</li>";
                    }
                    $("#tuan").html(optionstring);
                }
            }else{
                $("#tuan").html("<li>无数据</li> ");
            }
        }
        web.doAjax(root.inter.getSaleManagerByParam,'post',user_data, succCallBack);
    }else {
        var operate_manager=$("#operate_manager").val();
        var user_data = {
            keywords:operate_manager,
        }
        var succCallBack = function (data, status, response) {
            console.log(data)
            if(data.errorCode=="0"){
                var jsonObj =data.userList;
                if(jsonObj==""){
                    $("#tuan").html("<li>无数据</li> ");
                }else {
                    console.log(jsonObj)
                    var optionstring = "";
                    for (var  i= 0; i < jsonObj.length; i++) {
                        optionstring += "<li onclick='relation_x("+ jsonObj[i].USER_ID+",\""+jsonObj[i].NAME+"\")' data-align=\"" + jsonObj[i].ROLE_ID + "\" > " + jsonObj[i].NAME + "</li>";
                    }
                    $("#tuan_xiao").html(optionstring);
                }
            }else{
                $("#tuan_xiao").html("<li>无数据</li> ");
            }
        }
        web.doAjax(root.inter.getOperationManagerByParam,'post',user_data, succCallBack);
    }
}
//运营经理获取光标
$("#sale_manager").focus(function () {
    $("#tuan").show()
});
$(document).bind("click",function(e){
    //id为menu的是菜单，id为open的是打开菜单的按钮
    if($(e.target).closest("#tuan").length == 0 && $(e.target).closest("#sale_manager").length == 0){
        //点击id为menu之外且id不是不是open，则触发
        $("#tuan").hide()
    }
})
//销售经理获取光标
$("#operate_manager").focus(function () {
    $("#tuan_xiao").show()
});
$(document).bind("click",function(e){
    //id为menu的是菜单，id为open的是打开菜单的按钮
    if($(e.target).closest("#tuan_xiao").length == 0 && $(e.target).closest("#operate_manager").length == 0){
        //点击id为menu之外且id不是不是open，则触发
        $("#tuan_xiao").hide()
    }
})
function relation(id,ce) {
    $("#sale_manager").val(ce).text(id)
    $("#tuan").hide()
    console.log($("#sale_manager").text())
}
function relation_x(id,ce) {
    $("#operate_manager").val(ce).text(id)
    $("#tuan_xiao").hide()
    console.log($("#operate_manager").text())
}
$("#form-member").validate({
    rules: {
        kehu_id: {
            maxlength:10,
            // minlength:10,
        },
        kehu_name: {
            maxlength:50,
        },
        hetong: {
            maxlength:10,
            // minlength:10,
        },
    },
    submitHandler:function(form){
        getList(1)
    }
})
/*跳转去入驻*/
function goNextPage(title, url) {
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}

//初始化客户归属下拉列表
function ascription() {
    var user_data = {
    }
    var succCallBack = function (data, status, response) {
        //console.log(data)
        if(data.errorCode=="0") {
            var dataInter=data.group;
            var interText = doT.template($("#ascription_bob").text());
            $("#ascription_lsit").html(interText(dataInter));
        }
    };
    return web.doAjax(root.inter.getGroup, 'post', user_data, succCallBack);
}
var dataSource = new Array();
var table;   //table对象
dataSource = [];
function orderList(data) {
    dataSource.splice(0, dataSource.length);//清空数组
    var datashu =data.data.startIndex+1;
    if(data.data.result!=null){
    $.each(data.data.result, function (i, field) {
        var datashus = datashu++
        var rowData = new Array();
        rowData.push(field.contractNum);
        rowData.push(field.oldSysContractNum);
        rowData.push(field.contractType);
        rowData.push(field.conStartDate);
        rowData.push(field.conEndDate);
        rowData.push(field.customerName);//客户名称
        rowData.push(field.customerBelongGroupName);
        rowData.push(field.ftotalmount);
        rowData.push(field.credit);
        rowData.push(field.tickQuota);
        rowData.push(field.oldContractState);
        rowData.push(datashus);
        dataSource.push(rowData);
        rowData.push(field.customerCode);
        rowData.push(field.customerId);
    });
    }
    initDataTable(dataSource);
    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": false,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [25, 5, 10, 50],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                // {
                //     "visible": true,
                //     "targets": 0
                // },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[11]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        return val;
                    },
                    "targets": 2
                }, {
                    "render": function (data, type, row, meta) {
                        var val = row[2]

                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[3]
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[4]
                        return val;
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[5]
                        return val;
                    },
                    "targets": 6
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[6]
                        return val;
                    },
                    "targets": 7
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[7]
                        return val;
                    },
                    "targets": 8
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[8]
                        return val;
                    },
                    "targets": 9
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[9]
                        return val;
                    },
                    "targets":10
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[10]
                        return val;
                    },
                    "targets": 11
                },
                {
                    "render": function (data, type, row, meta) {
                        console.log(row[12] + 'dss')
                        if(row[2]=='购销协议'){
                            var val = '<a class="powerButton 165" onclick="goNextPage(\'合同详情\',\'../contract/contr_detail.html?customerCode=' + row[12] + '&contractNum=' + row[0] + '\')">查看详情</a>';
                            return val;
                        }else if(row[2]=='虚拟合同'){
                            var val = '<a class="powerButton 165" onclick="goNextPage(\'合同详情\',\'../contract/xuni_detail.html?customerCode=' + row[12] + '&contractNum=' + row[0] + '\')">查看详情</a>';
                            return val;
                        }else if(row[2]=='单笔合同'){
                            var val = '<a class="powerButton 165" onclick="goNextPage(\'合同详情\',\'../contract/dan_detail.html?customerCode=' + row[12] + '&contractNum=' + row[0] + '\')">查看详情</a>';
                            return val;
                        }if(row[2]=='客户资信调查表'){
                            var val = '<a class="powerButton 165" onclick="goNextPage(\'合同详情\',\'../contract/contr_detail.html?customerCode=' + row[12] + '&contractNum=' + row[0] + '\')">查看详情</a>';
                            return val;
                        }
                    },
                    "targets": 12
                }
            ]
        });
        //权限控制函数
        power()
    };
}
function getList(page_num){
    console.log($("#contract_state").val())
    var kehu_id=$("#kehu_id").val(),
        kehu_name=$("#kehu_name").val(),
        hetong=$("#hetong").val(),
        ascription_lsit=$("#ascription_lsit").val(),
        check_state=$("#check_state").val(),
        contract_state=$("#contract_state").val(),
        sale_manager=$("#sale_manager").text(),
        logmin=$("#logmin").val(),
        logmax=$("#logmax").val(),
        operate_manager=$("#operate_manager").text();
    var user_data = {
        customerId:kehu_id,
        customerName:kehu_name,
        contractNum:hetong,
        customerBelongGroupId:ascription_lsit,
        contractType:check_state,
        customerSaleDirectorId:sale_manager,
        oldContractState:contract_state,
        customerManagerId:operate_manager,
        conEndDate:logmax,
        conStartDate:logmin,
        pageNum:page_num,
        pageSize:50,
       /* areaIds:sessionStorage.getItem("areaId"),//仓库权限串*/
        organizationIds:sessionStorage.getItem("companya"),//销售归属权限串
        salesSupervisorUserIds:sessionStorage.getItem("personnela"),//销售经理权限串

    }
    var succCallBack = function (data, status, response) {
        console.log(data)
        if(data.errorCode=="0") {
            if(data.list==''){
                var dttable = $('#datatable').dataTable( ); // 初始化dataTable，清空上一个值
                dttable.fnClearTable(); //清空一下table
            }
            var totalPagess=""
            if(data.data.result!=""){
                totalPagess=data.data.totalPage
            }else {
                totalPagess=0
            }
            orderList(data);
            $('.M-box3').pagination({
                pageCount:totalPagess,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    $.ajax({
                        type: "POST",
                        /*root.inter.findContractInfoByPage*/
                        url:root.inter.findContractInfoByPage,
                        cache: false,  //禁用缓存
                        dataType: "json",
                        data: {
                            customerId:kehu_id,
                            customerName:kehu_name,
                            contractNum:hetong,
                            customerBelongGroupId:ascription_lsit,
                            contractType:check_state,
                            customerSaleDirectorId:sale_manager,
                            oldContractState:contract_state,
                            customerManagerId:operate_manager,
                            conEndDate:logmax,
                            conStartDate:logmin,
                            pageNum:api.getCurrent(),
                            pageSize:50,
                            /*areaIds:sessionStorage.getItem("areaId"),//仓库权限串*/
                            organizationIds:sessionStorage.getItem("companya"),//销售归属权限串
                            salesSupervisorUserIds:sessionStorage.getItem("personnela"),//销售经理权限串
                        },
                        success: function (result) {
                            if(result.errorCode=="0"){
                                orderList(result);
                            }else {
                                layer.msg(result.errorMessage, {icon: 5,time:1000});
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });
        }else{
            var dttable = $('#datatable').dataTable( ); // 初始化dataTable，清空上一个值
            dttable.fnClearTable(); //清空一下table
            return false
        }
    };
    web.doAjax(root.inter.findContractInfoByPage, 'post', user_data, succCallBack);
}





